package com.cumt.threeway.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cumt.threeway.R;
import com.cumt.threeway.adapter.ClassificationAdapter;
import com.cumt.threeway.model.Item;
import com.cumt.threeway.view.widget.ShelfTabView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jingjing.Hu on 2016/4/1.
 */
public class ClassificationFragment extends Fragment implements OnClickListener,ViewPager.OnPageChangeListener,ShelfTabView.ItemClick{
    private TextView mSingleTextView;
    private TextView mGoWithTextView;
    private TextView mLoversTextView;
    private TextView mFamilyTextView;
    private ViewPager mViewPager;
    private ShelfTabView mStoreTabView;
    private int mPosition;
    private int mCurrentTagId = -1;
    private SingleFragment mSingleFragment;
    private GoWithFragment mGoWithFragment;
    private LoversFragment mLoversFragment;
    private FamilyFragment mFamilyFragment;

    private Handler mHandler = new Handler();
    public static ClassificationFragment newInstance() {
        return new ClassificationFragment();
    }


    // 继oncreateview后面执行
    @Override
    public void onStart() {
        super.onStart();
        if (mViewPager != null) {
            // 当页面可见是赋值
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mViewPager.setCurrentItem(mPosition);
                }
            });
        }
    }

    // 设置默认选项为单人行
    public void changeSingleTextColor() {
        mSingleTextView.setTextColor(getResources().getColor(R.color.green_dark));
        mGoWithTextView.setTextColor(getResources().getColor(R.color.black_1));
        mLoversTextView.setTextColor(getResources().getColor(R.color.black_1));
        mFamilyTextView.setTextColor(getResources().getColor(R.color.black_1));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classification_layout, null, false);
        initView(view);
        initData();
        // 设置默认选项为单人行
        changeSingleTextColor();
        return view;
    }

    private void initData() {
        List<Fragment> list = new ArrayList<Fragment>();
        mSingleFragment = new SingleFragment();
        mGoWithFragment = new GoWithFragment();
        mLoversFragment = new LoversFragment();
        mFamilyFragment = new FamilyFragment();
        list.add(mSingleFragment);
        list.add(mGoWithFragment);
        list.add(mLoversFragment);
        list.add(mFamilyFragment);

        ArrayList<Item> items = new ArrayList<Item>();
        items.add(new Item(1, "个人活动"));
        items.add(new Item(2, "团队活动"));
        items.add(new Item(3, "情侣活动"));
        items.add(new Item(4, "家庭活动"));
        mStoreTabView.setItems(items);
        mStoreTabView.setItemClickListener(this);
        ClassificationAdapter classificationAdapter = new ClassificationAdapter(getFragmentManager(), getActivity(), list);
        mViewPager.setAdapter(classificationAdapter);
    }

    private void initView(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.vp);
        mStoreTabView = (ShelfTabView) view.findViewById(R.id.shelf_tab);
        mSingleTextView = (TextView) view.findViewById(R.id.tv_single);
        mGoWithTextView = (TextView) view.findViewById(R.id.tv_gowith);
        mLoversTextView = (TextView) view.findViewById(R.id.tv_lovers);
        mFamilyTextView = (TextView) view.findViewById(R.id.tv_family);
        mViewPager.addOnPageChangeListener(this);
        mSingleTextView.setOnClickListener(this);
        mGoWithTextView.setOnClickListener(this);
        mLoversTextView.setOnClickListener(this);
        mFamilyTextView.setOnClickListener(this);
    }

    public void setPageItem(int position) {
        mPosition = position;
        if(mViewPager != null) {
            mViewPager.setCurrentItem(position);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_single:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.tv_gowith:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.tv_lovers:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.tv_family:
                mViewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(int tagId) {
        int position = mStoreTabView.getPositionByTagId(tagId);
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mStoreTabView.onPageScrolled(position, positionOffset);
    }

    @Override
    public void onPageSelected(int position) {
        mStoreTabView.changeSelect(position);
        mCurrentTagId = mStoreTabView.getItem(position).getId();
        switch (position) {
            case 0:
                changeSingleTextColor();
                break;
            case 1:
                changeGoWithTextColor();
                break;
            case 2:
                changeLoversTextColor();
                break;
            case 3:
                changeFamilyTextColor();
                break;
            default:
                break;
        }
    }

    private void changeFamilyTextColor() {
        mSingleTextView.setTextColor(getResources().getColor(R.color.black_1));
        mGoWithTextView.setTextColor(getResources().getColor(R.color.black_1));
        mLoversTextView.setTextColor(getResources().getColor(R.color.black_1));
        mFamilyTextView.setTextColor(getResources().getColor(R.color.green_dark));
    }

    private void changeLoversTextColor() {
        mSingleTextView.setTextColor(getResources().getColor(R.color.black_1));
        mGoWithTextView.setTextColor(getResources().getColor(R.color.black_1));
        mLoversTextView.setTextColor(getResources().getColor(R.color.green_dark));
        mFamilyTextView.setTextColor(getResources().getColor(R.color.black_1));
    }

    private void changeGoWithTextColor() {
        mSingleTextView.setTextColor(getResources().getColor(R.color.black_1));
        mGoWithTextView.setTextColor(getResources().getColor(R.color.green_dark));
        mLoversTextView.setTextColor(getResources().getColor(R.color.black_1));
        mFamilyTextView.setTextColor(getResources().getColor(R.color.black_1));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
